<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="icon" href="logo.ico">
    <title>添加商品</title>
    <link rel="stylesheet" href="css/eui.css">
</head>
<body>

<div id="app">
    <!--页头组件-->
    <el-page-header style="background-color: #467097;color: white;line-height: 60px"
            @back="goBack" content="发布商品页面"></el-page-header>
    <el-divider></el-divider>
    <el-card>
        <el-form>
            <el-form-item label="商品标题" label-width="80px">
                <el-input v-model="pro.title"></el-input>
            </el-form-item>
            <el-form-item label="价格" label-width="80px">
                <el-input v-model="pro.price"></el-input>
            </el-form-item>
            <el-form-item label="商品库存" label-width="80px">
                <el-input v-model="pro.stock"></el-input>
            </el-form-item>
            <el-form-item label="销量" label-width="80px">
                <el-input v-model="pro.saleCount"></el-input>
            </el-form-item>
            <el-form-item label="商品描述" label-width="80px">
                <el-input v-model="pro.detail"></el-input>
            </el-form-item>
            <el-form-item label="分类" label-width="80px">
                <template>
                    <el-select v-model="pro.categoryId" placeholder="请选择">
                        <el-option v-for="c in cate"
                                   :label="c.name"
                                   :value="c.id">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item>
            <el-form-item label="商品图片" label-width="80px">
                <el-upload
                        action="/upload"
                        name="picFile"
                        :limit="1"
                        list-type="picture-card"
                        :on-preview="hand"
                        :on-success="handleSuccess"
                        :on-remove="handleRemove">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>
        </el-form>
        <el-button @click="insert()">发布</el-button>
    </el-card>
</div>
</body>
<script src="js/vue.js"></script>
<script src="js/eui.js"></script>
<script src="js/axios.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                pro:{
                    title:"",
                    url:"",
                    price:0,
                    stock:0,
                    saleCount:0,
                    detail:0,
                    categoryId:""
                },
                cate:[]
            }
        },
        methods: {
            handleRemove(file, fileList) {
                console.log(file, fileList);
                axios.get("/remove?name="+file.response).then(function (response) {
                    v.$message.success("删除成功");
                })
            },
            hand(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            insert(){
                if (v.pro.url==""){
                    alert("图片不能为空!")
                    return;
                }
                axios.post("/pro/insert",v.pro).then(function (response) {
                        alert("添加完成!");
                        location.href="/admin.html"; //显示管理页面
                })
            },
            handleSuccess(response,file,fileList){
                v.pro.url = response;
            },
            goBack(){
                history.back();
            }
        },
        created:function () {
            axios.get("/cate/select").then(function (response) {
                v.cate = response.data;
            })
        }
    })
</script>
</html>